﻿using Aribaccio.Copyright;
using BDPAutodetect.Client.ViewWindows.WindowDialog;
using BDPAutodetect.Mgrcore;
using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace BDPAutodetect.Client
{
    /// <summary>
    /// 
    /// </summary>
    public partial class SettingSystemViewModel : BaseViewModel<UserControl>
    {
        /// <summary>
        /// 程序加载实例
        /// </summary>
        protected AppLoader Loader { get; set; }
        /// <summary>
        /// 用户管理实例
        /// </summary>
        protected UserManager UserMgr { get; set; }
        /// <summary>
        /// 系统参数管理实例
        /// </summary>
        protected SettingSystemManager SettingSystemMgr { get; set; }

        /// <summary>
        /// 当前登录用户信息
        /// </summary>
        [ObservableProperty]
        protected MUserDto? userData;
        /// <summary>
        /// 是否可以编辑
        /// </summary>
        [ObservableProperty]
        protected bool isCheckedEditor = false;
        /// <summary>
        /// 是否选择显示明文密码
        /// </summary>
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(IsVisibilityPassword))]
        protected bool isCheckedPassword = false;
        /// <summary>
        /// 是否显示明文密码 
        /// </summary>
        public Visibility IsVisibilityPassword => this.IsCheckedPassword ? Visibility.Visible : Visibility.Hidden;

        /// <summary>
        ///  期限天数
        /// </summary>
        [ObservableProperty]
        protected int limitSurplusDays = 0;
        /// <summary>
        /// 期限天数显示文本
        /// </summary>
        [ObservableProperty]
        protected string limitSurplusDaysDisplay = "试用天数";
        /// <summary>
        /// 期限天数画刷颜色
        /// </summary>
        [ObservableProperty]
        protected Brush limitSurplusDaysBackground = new SolidColorBrush(Colors.Red);

        /// <summary>
        /// 扫码枪启用禁止状态
        /// </summary>
        [ObservableProperty]
        protected EnableStatus barcodeEnableStatus = EnableStatus.Disabled;
        /// <summary>
        /// 版权期限启用禁止状态
        /// </summary>
        [ObservableProperty]
        protected EnableStatus copyLimitsEnableStatus = EnableStatus.Disabled;
        /// <summary>
        /// 是否可以显示按钮
        /// </summary>
        [ObservableProperty]
        protected Visibility isVisibility = Visibility.Hidden;

        /// <summary>
        /// 构造函数
        /// </summary>
        public SettingSystemViewModel(AppLoader argAppLoader, UserManager argUserManager, SettingSystemManager argSystemManager)
        {
            this.Loader = argAppLoader;
            this.UserMgr = argUserManager;
            this.UserData = argUserManager.LoginUser;
            this.SettingSystemMgr = argSystemManager;
        }

        /// <summary>
        /// 页面初始化加载事件
        /// </summary>
        [RelayCommand]
        public void ExecuteLoaded()
        {
            if (this.IsLoaded) return;
            //更新是否显示状态
            if (this.UserMgr.LoginUser?.Supered > 0)
            {
                this.IsVisibility = Visibility.Visible;
            }
            //参数设置消息, 扫码枪启用状态
            var bIsBarcode = this.SettingSystemMgr.GetSettingTypeData<bool>(SettingType.EnableBarcode, true);
            this.BarcodeEnableStatus = bIsBarcode ? EnableStatus.Enabled : EnableStatus.Disabled;
            //参数设置消息, 版权期限启用状态
            var bIsCopyLimits = this.SettingSystemMgr.GetSettingTypeData<bool>(SettingType.EnableCopyLimits, true);
            this.CopyLimitsEnableStatus = bIsCopyLimits ? EnableStatus.Enabled : EnableStatus.Disabled;
            //版权消息
            var registerCopyright = this.Loader.GetCopyrightLimits();
            if (registerCopyright != null)
            {
                this.LimitSurplusDays = registerCopyright.LimitSurplusDays;
                //设置显示标题信息
                switch (registerCopyright.Status)
                {
                    case RegisterCopyStatus.OK:
                    case RegisterCopyStatus.OKRegister:
                        this.LimitSurplusDaysDisplay = "有效天数";
                        break;
                    case RegisterCopyStatus.OKTimeLimits:
                    case RegisterCopyStatus.None:
                    default:
                        this.LimitSurplusDaysDisplay = "试用天数";
                        break;
                }
                //设置背景颜色
                if (registerCopyright.LimitSurplusDays <= 7)
                {
                    this.LimitSurplusDaysBackground = new SolidColorBrush(Colors.Red);
                }
                else if (registerCopyright.LimitSurplusDays > 7 && registerCopyright.LimitSurplusDays <= 15)
                {
                    this.LimitSurplusDaysBackground = new SolidColorBrush(Colors.Yellow);
                }
                else
                {
                    this.LimitSurplusDaysBackground = new SolidColorBrush(Colors.Green);
                }
            }
            //标记初始化加载完成
            this.IsLoaded = true;
        }

        /// <summary>
        /// 编辑保存命令事件
        /// </summary>
        /// <returns></returns>
        [RelayCommand]
        public async Task ExecuteSave(MUserDto argUserDto)
        {
            if (this.UserData == null || !this.IsCheckedEditor) return;
            this.IsExecutedIdle = false;
            try
            {
                //确认弹出层
                if (MessageBox.Show("您确定编辑保存？", "警告", MessageBoxButton.YesNo) != MessageBoxResult.Yes) return;
                if (string.IsNullOrWhiteSpace(this.UserData.Name))
                {
                    MessageBox.Show("名称不能为空", "警告", MessageBoxButton.OK);
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.UserData.NameNick))
                {
                    MessageBox.Show("别称不能为空", "警告", MessageBoxButton.OK);
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.UserData.PasswordDisplay))
                {
                    MessageBox.Show("密码不能为空", "警告", MessageBoxButton.OK);
                    return;
                }

                //修改保存
                var result = this.UserMgr.UpdateUser(this.UserData);
                await result.ConfigureAwait(false);
                if (result == null || !result.Result)
                {
                    MessageBox.Show("修改失败", "警告", MessageBoxButton.OK);
                }
            }
            finally
            {
                this.IsExecutedIdle = true;
            }
        }

        /// <summary>
        /// 编辑保存参数命令事件
        /// </summary>
        /// <returns></returns>
        [RelayCommand]
        public async Task ExecuteSaveSetting()
        {
            this.IsExecutedIdle = false;
            try
            {
                bool bIsBarcode = this.BarcodeEnableStatus == EnableStatus.Enabled;
                var result = this.SettingSystemMgr.UpdateSettingData(SettingType.EnableBarcode, bIsBarcode);
                await result.ConfigureAwait(false);
                if (result == null || !result.Result)
                {
                    MessageBox.Show("修改参数失败", "警告", MessageBoxButton.OK);
                }
            }
            finally
            {
                this.IsExecutedIdle = true;
            }
        }

    }
}
